package win.bojack.mall.product.service.impl;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import win.bojack.common.utils.PageUtils;
import win.bojack.common.utils.Query;

import win.bojack.mall.product.dao.CategoryDao;
import win.bojack.mall.product.entity.CategoryEntity;
import win.bojack.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationServiceImpl categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 1, 查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        // 使用递归，组装父子结构：
        return categoryEntities.stream().filter((categoryEntity) -> categoryEntity.getParentCid() == 0)
                .peek((entity -> entity.setChildren(getChildren(entity, categoryEntities))))
                .sorted((e1, e2) -> (e1.getSort() == null ? 0 : e1.getSort()) - (e2.getSort() == null ? 0 : e2.getSort()))
                .collect(Collectors.toList());
    }

    @Override
    public void removeValid(List<Long> asList) {
        // TODO
        // 1、删除之前检查菜单是否被其他功能引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] getCategoryIdsById(Long catelogId) {
        List<Long> categoryIds = Lists.newArrayList();
        List<Long> categoryIds_ = getCategoryIdsRecursively(catelogId, categoryIds);
        Collections.reverse(categoryIds_);
        return categoryIds_.toArray(new Long[categoryIds_.size()]);
    }


    @Override
    @Transactional
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
    }

    private List<Long> getCategoryIdsRecursively(Long categoryId, List<Long> categoryIds) {
        if (categoryId != 0) {
            categoryIds.add(categoryId);
            CategoryEntity byId = this.getById(categoryId);
            getCategoryIdsRecursively(byId.getParentCid(), categoryIds);
        }
        return categoryIds;
    }

    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        return all.stream().filter((entity -> entity.getParentCid().equals(root.getCatId())))
                .sorted((e1, e2) -> (e1.getSort() == null ? 0 : e1.getSort()) - (e2.getSort() == null ? 0 : e2.getSort()))
                .peek((entity -> entity.setChildren(getChildren(entity, all))))
                .collect(Collectors.toList());
    }
}